/**
 * @file HistoryData.h
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-10-12
 * 
 * @copyright Copyright (c) 2024  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-10-12     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#include "stdint.h"
#include <stdbool.h>

#ifndef _HISTORY_DATA_H_
#define _HISTORY_DATA_H_


#ifdef __cplusplus
extern "C"
{
#endif

#define UTC_OFFSET_1970_2000 946684800


typedef struct
{
    union
    {
        struct
        {
            unsigned char syncHistory : 1;         //
            unsigned char syncInit  : 1;         //
        } Bits;
        uint8_t Bytes;
    } Flag;
    uint32_t askStartTime;
    uint32_t flashReadAddr;
}HistorySync_t;

typedef struct
{
    uint32_t utcTime;
    uint32_t SumHumidity;
    uint16_t SumHumidityCnt;
    uint8_t MinHumidity;
    uint8_t MaxHumidity;
    int32_t SumTemperature;
    uint16_t SumTemperatureCnt;
    int16_t MinTemperature; // 1 as 0.1℃
    int16_t MaxTemperature; // 1 as 0.1℃
} TempHumidityStatistic_t;

typedef struct
{
    uint32_t utcTime;
    uint16_t Humidity;
    int16_t Temperature; // 1 as 0.1℃
} TempHumidityHistory_t;
#define SINGLE_HISTORY_DATA_LEN 8 // 必须大于等于TempHumidityHistory_t结构体大小，而且必须能被4整除
//------------------------------------------
/**
 * 用户历史记录存储，地址空间使用0x6C000 - 73FFFF --> 32K
 */
#define HISTORY_START_ADDR 0x6C000
#define HISTORY_END_ADDR 0x74000 // <
#define HISTORY_MAX_SIZE (HISTORY_END_ADDR - HISTORY_START_ADDR)
#define FLASH_SECTOR_SIZE 4096
#define HISTORY_MAX_SECTOR HISTORY_MAX_SIZE / FLASH_SECTOR_SIZE

typedef struct
{
    uint32_t StartAddr;
    uint32_t CurrentAddr;
    int8_t EmptySectorCnt;
} FlashHistory_t;

void InitFlashHistoryIndex(void);
void TestHistory(void);

#ifdef __cplusplus
}
#endif

#endif